﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;
using System.Windows.Forms;
using System.Collections;


namespace WLC
{
    class UpdateStatisticStepDefinition : IStepDefinition
    {
        #region IStepDefinition Members

        public IStep CreateStep(IPropertyReaders properties)
        {
            return new UpdateStatisticStep(properties);
        }

        public void DefineSchema(IPropertyDefinitions schema)
        {
            IPropertyDefinition statisticElement = schema.AddElementProperty("StatisticElement", StatisticElementDefinition.ID);
            statisticElement.Description = "The statistics element that manages the statistics for this model.";
            statisticElement.Required = true;

            IPropertyDefinition statisticName = schema.AddStringProperty("StatisticName", string.Empty);
            statisticName.Description = "The name of the statistic to update.";
            statisticName.Required = true;

            IPropertyDefinition value = schema.AddExpressionProperty("Value", string.Empty);
            value.Description = "The value to add to the statistic.";
            value.Required = true;
        }

        public int NumberOfExits
        {
            get { return 1; }
        }

        public string Name
        {
            get { return "UpdateStatistic"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }

        public Guid UniqueID
        {
            get { return new Guid("{141EF4AC-6EFA-11DF-9A42-2913E0D72085}"); }
        }

        #endregion
    }


    class UpdateStatisticStep : IStep
    {

        IPropertyReader _statisticName;
        IPropertyReader _valueProp;
        IElementProperty _statisticElementProp;


        public UpdateStatisticStep(IPropertyReaders bag)
        {
            _statisticName = bag.GetProperty("StatisticName");
            _valueProp = bag.GetProperty("Value");
            _statisticElementProp = (IElementProperty)bag.GetProperty("StatisticElement");

        }

        #region IStep Members

        private string GetOrderType(IStepExecutionContext context)
        {
            string rawType = context.AssociatedObject.ExecutionContext.ToString();
            string[] splitType = rawType.Split(' ');


            return splitType[1];
        }

        public ExitType Execute(IStepExecutionContext context)
        {


            string orderType = GetOrderType(context);
            string statisticName = _statisticName.GetStringValue(context);
            double value = _valueProp.GetDoubleValue(context);
            StatisticElement statisticElement = (StatisticElement)_statisticElementProp.GetElement(context);

            statisticElement.AddStatisticValue(statisticName, value);

            return ExitType.FirstExit;
        }

        #endregion

    }


}
